from rest_framework import serializers


from goods.models import SKU


class CartSelectAllSerializer(serializers.Serializer):
    #购物车全选序列化器
    selected = serializers.BooleanField(label='全选')



class CartDeleteSerializer(serializers.Serializer):
    #删除购物车序列化器
    sku_id = serializers.IntegerField(label='商品ID', min_value=1)

    def validate_sku_id(self, value):

        try:

            sku = SKU.objects.get(id=value)

        except SKU.DoesNotExist:

            raise serializers.ValidationError('商品不存在')

        return value


class CartSKUSerializer(serializers.ModelSerializer):
    #修改购物车序列化器
    count = serializers.IntegerField(label='商品数量', min_value=1)

    selected = serializers.BooleanField(label='是否勾选', default=True)


    class Meta:

        model = SKU

        fields = ['id', 'default_image_url', 'name', 'price', 'selected', 'count']


class CartSerializer(serializers.Serializer):

    """
    购物车序列化器
    """

    sku_id = serializers.IntegerField(label='商品ID', min_value=1)

    count = serializers.IntegerField(label='数量', min_value=1)

    selected = serializers.BooleanField(label='是否勾选', default=True)

    def validate_sku_id(self, value):

        try:

            sku = SKU.objects.get(id=value)

        except SKU.DoesNotExist:

            raise serializers.ValidationError('商品不存在')

        return value